'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // Create interest_tags table
    await queryInterface.createTable('interest_tags', {
      id: {
        type: Sequelize.INTEGER.UNSIGNED,
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
      },
      name: {
        type: new Sequelize.STRING(128),
        allowNull: false,
        unique: true,
        comment: '兴趣标签名称'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    }, { ignoreDuplicates: true });// 在这里添加或确认存在 ignoreDuplicates: true

    // Create UserInterestTag join table
    await queryInterface.createTable('UserInterestTag', {
      userId: {
        type: Sequelize.STRING(36),
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        primaryKey: true,
        allowNull: false,
        charset: 'utf8',
        collate: 'utf8_bin',
      },
      tagId: {
        type: Sequelize.INTEGER.UNSIGNED,
        references: {
          model: 'interest_tags',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        primaryKey: true,
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },

  async down(queryInterface, Sequelize) {
    // Drop tables in reverse order of creation
    await queryInterface.dropTable('UserInterestTag');
    await queryInterface.dropTable('interest_tags');
  }
};
